/**
 * Filename: TodayShowActivity.java
 *
 * @author Firefly
 * @contact postfirefly@163.com
 * @time 2016/3/9 2:57
 */
package com.github.postsanf.yinian.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.adapter.MainFragmentAdapter;
import com.github.postsanf.yinian.bean.YNTodayM;
import com.github.postsanf.yinian.entity.YNTodayMResponse;
import com.github.postsanf.yinian.fragment.TodayShowFragment;
import com.github.postsanf.yinian.utils.ReqUtils;
import com.github.postsanf.yinian.utils.UIHelper;
import com.github.postsanf.yinian.widget.SwipeBackLayout;
import com.github.postsanf.yinian.widget.SwipeBackTodayShowActivity2;
import com.litesuits.http.exception.HttpException;
import com.litesuits.http.listener.HttpListener;
import com.litesuits.http.request.StringRequest;
import com.litesuits.http.request.param.HttpMethods;
import com.litesuits.http.response.Response;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * 今日忆页面
 */
public class TodayShowActivity extends SwipeBackTodayShowActivity2 {
    private ViewPager vp_today_show;
    private ImageView btn_up_exit;
    private List<YNTodayM> mDatas = new ArrayList<>();
    private SwipeBackLayout swipeBackLayout;
    private List<Fragment> fragments = new ArrayList<>();
    private ImageView indicator_iv_left;
    private ImageView indicator_iv_right;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.yn_activity_today_show2);
        setDragEdge(SwipeBackLayout.DragEdge.BOTTOM);
        initView();
        initData();
        swipeBackLayout = getSwipeBackLayout();
    }

    @Override
    public SwipeBackLayout getSwipeBackLayout() {
        return super.getSwipeBackLayout();
    }

    private void initData() {
        LinkedHashMap<String, String> mParam = new LinkedHashMap<>();
        StringRequest req = new StringRequest("https://api.zhuiyinanian.com/YinianProject/h5/ShowTodayMemoryInThreeDay")
                .setMethod(HttpMethods.Post)
                .setParamMap(mParam)
                .setHttpListener(new HttpListener<String>(true, false, true) {
                    @Override
                    public void onSuccess(String s, Response<String> response) {
                        YNTodayMResponse resToday = gson.fromJson(s, YNTodayMResponse.class);
                        if (resToday != null && ReqUtils.isSuccess(resToday.getCode())) {
                            List<YNTodayM> list = resToday.getData();
                            mDatas.add(list.get(list.size() - 1));
                            mDatas.add(list.get(list.size() - 2));
                            if (mDatas.size() > 0) {
                                for (int i = 0; i < mDatas.size(); i++) {
                                    TodayShowFragment todayShowFragment = new TodayShowFragment(TodayShowActivity.this, mDatas.get(i), i);
                                    fragments.add(todayShowFragment);
                                }
                                vp_today_show.setAdapter(new MainFragmentAdapter(getSupportFragmentManager(), fragments));
                                vp_today_show.setCurrentItem(1);
                            }
                        } else {
                            showToast(resToday.getMsg());
                        }
                    }

                    @Override
                    public void onFailure(HttpException e, Response<String> response) {
//                        showToast(getString(R.string.YN_NET_LINK));
                    }
                });
        liteHttp.executeAsync(req);
    }

    float downX = 0f;
    float downY = 0f;
    float x = 0f;
    float y = 0f;

    private void initView() {
        vp_today_show = (ViewPager) findViewById(R.id.vp_today_show);
        btn_up_exit = (ImageView) findViewById(R.id.today_show_up);
        indicator_iv_left = (ImageView) findViewById(R.id.indicator_iv_left);
        indicator_iv_right = (ImageView) findViewById(R.id.indicator_iv_right);
        btn_up_exit.startAnimation(getBGAnimation(R.anim.anim_today_up));
        vp_today_show.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            private int lastValue = -1;

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                Log.i("xjs", "positionOffset==>" + positionOffset);
                if (positionOffset != 0) {
                    if (lastValue >= positionOffsetPixels) {
                        //右滑
                        indicator_iv_left.setImageResource(R.drawable.iv_yellow_round);
                        indicator_iv_right.setImageResource(R.drawable.iv_gray_round);
                    } else if (lastValue < positionOffsetPixels) {
                        //左滑
                        indicator_iv_left.setImageResource(R.drawable.iv_gray_round);
                        indicator_iv_right.setImageResource(R.drawable.iv_yellow_round);
                    }
                }
                lastValue = positionOffsetPixels;
                if (positionOffset > 0) {
                    int ww = UIHelper.dip2px(TodayShowActivity.this, 6) + (int) (UIHelper.dip2px(TodayShowActivity.this, 7) * (1 - positionOffset));
                    indicator_iv_left.setLayoutParams(new LinearLayout.LayoutParams(ww, LinearLayout.LayoutParams.MATCH_PARENT));
                }
            }

            @Override
            public void onPageSelected(int position) {
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
        btn_up_exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                intent2Activity(MainActivity.class);
                overridePendingTransition(R.anim.none_in, R.anim.slide_up_out);
                finish();
            }
        });
        vp_today_show.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        downX = event.getX();
                        downY = event.getY();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        float moveX = event.getX();
                        float moveY = event.getY();
                        x = Math.abs(moveX - downX);
                        y = Math.abs(moveY - downY);
                        if (x >= 0.5 * y) {
                            swipeBackLayout.setEnableFlingBack(false);
                            swipeBackLayout.setEnablePullToBack(false);
                        } else {
                            swipeBackLayout.setEnableFlingBack(true);
                            swipeBackLayout.setEnablePullToBack(true);
                        }
                        break;
                    case MotionEvent.ACTION_UP:
                        swipeBackLayout.setEnableFlingBack(false);
                        swipeBackLayout.setEnablePullToBack(false);
                        break;
                }
                return false;
            }
        });
        findViewById(R.id.today_show_share).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mDatas.size() >= 2) {
                    Intent intent = new Intent(TodayShowActivity.this, TodayShareActivity2.class);
                    intent.putExtra("TMpic", mDatas.get(vp_today_show.getCurrentItem()).getTMpic());
                    intent.putExtra("TMtext", mDatas.get(vp_today_show.getCurrentItem()).getTMtext());
                    intent.putExtra("CurrentItem", vp_today_show.getCurrentItem());
                    startActivity(intent);
                } else {
                    Toast.makeText(TodayShowActivity.this, "数据初始化失败", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }


    // 根据动画ID获取Animation
    private Animation getBGAnimation(int resID) {
        Animation animation = AnimationUtils.loadAnimation(this, resID);
        return animation;
    }


    @Override
    public void onBackPressed() {
        super.onBackPressed();
        overridePendingTransition(R.anim.none_in, R.anim.slide_up_out);
        finish();
    }

    // 友盟统计
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(this.getClass().getSimpleName());
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(this.getClass().getSimpleName());
        MobclickAgent.onPause(this);
    }
}
